package cn.tedu._04weibo.base.interceptors;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class LoginCheckInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        log.debug("开始执行LoginCheckInterceptor的preHandle方法");
        // 获取当前用户登录信息
        Object user = request.getSession().getAttribute("user");
        if (user == null) {
            // 用户没有登录
            log.debug("用户没有登录");
            // 跳转到登录页面
            response.sendRedirect("/login.html");
            return false;
        }
        log.debug("用户已经登录，放行");
        return true;
    }
}
